package com.shishuo.cms.action.manage;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shishuo.cms.action.ClassesIndexAction;
import com.shishuo.cms.auth.AuthPassport;
import com.shishuo.cms.constant.UploadUtils;
import com.shishuo.cms.entity.ClassesEntity;
import com.shishuo.cms.entity.GoodsItemEntity;
import com.shishuo.cms.entity.GoodsItemKeywordsEntity;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.GoodsItemKeywordsService;
import com.shishuo.cms.service.GoodsItemService;

@Controller
@RequestMapping("/admin/goods")
public class GoodsItemAction extends AdminBaseAction {
	@Autowired
	private GoodsItemService goodsItemService;
	
	@Autowired
	private ClassesIndexAction classesIndexAction;
	@Autowired
	private GoodsItemKeywordsService goodsItemKeywordsService;
	
	@InitBinder("goodsItemEntity")
	public void initBinder(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("goodsItemEntity.");
	}
	
	
	@AuthPassport
	@RequestMapping(value = "/add.htm", method = RequestMethod.GET)
	public String addUser(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("goods");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		List<GoodsItemKeywordsEntity> listGoodsItemKeywordsEntity=goodsItemKeywordsService.getAllList(0, 10);
		modelMap.put("listGoodsItemKeywordsEntity", listGoodsItemKeywordsEntity);
		return "system/goods/add";
	}
	@AuthPassport
	@RequestMapping(value = "/import.htm", method = RequestMethod.GET)
	public String importGoodsItem(ModelMap modelMap) throws Exception {
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("goods");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/goods/import";
	}
	
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/importGoods.json", method = RequestMethod.POST)
	public JsonVo<String> importGoods(
			@ModelAttribute GoodsItemEntity goodsItemEntity,
			@RequestParam("upfile") MultipartFile[] upfile)
			throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (upfile== null) {
			json.getErrors().put("upfile", "上传文件不能为空");
		}
		if (goodsItemEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		Date now = new Date();
		String basePath = request.getSession().getServletContext().getRealPath("/");
		for (int i = 0; i < upfile.length; i++) {
			String uploadPath = UploadUtils.getUploadPath(basePath,upfile[i].getOriginalFilename(), now.getTime());
			upfile[i].transferTo(new java.io.File(basePath+uploadPath));
			goodsItemEntity.setStatus(1);
			goodsItemService.importGoods(goodsItemEntity,basePath+uploadPath);
		}
		json.setResult(true);
		return json;
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public JsonVo<String> addNewGoods(
			@ModelAttribute GoodsItemEntity goodsItemEntity,
			@RequestParam(value = "content", defaultValue = "") String content)
			throws Exception {
		goodsItemEntity.setContent(content);
		JsonVo<String> json = new JsonVo<String>();
		if (goodsItemEntity.getTitle() == null
				|| "".equals(goodsItemEntity.getTitle())) {
			json.getErrors().put("title", "产品名称不能为空");
		}
		if (goodsItemEntity.getContent() == null
				|| "".equals(goodsItemEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (goodsItemEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		goodsItemService.insert(goodsItemEntity);
		json.setResult(true);
		return json;
	}

	@AuthPassport
	@RequestMapping(value = "/page.htm", method = RequestMethod.GET)
	public String allList(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) throws Exception {
		modelMap.put("p", pageNum);
		modelMap.put("pageVo", goodsItemService.getAllListPage(pageNum,0));
		return "system/goods/list";
	}

	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.GET)
	public JsonVo<String> delete(
			@RequestParam(value = "goodsId") long goodsId) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		goodsItemService.deleteById(goodsId);
		json.setResult(true);
		return json;
	}

	
	@AuthPassport
	@RequestMapping(value = "/update.htm", method = RequestMethod.GET)
	public String update(@RequestParam(value = "goodsId") long goodsId,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		GoodsItemEntity goodsItemEntity=goodsItemService.getById(goodsId);
		modelMap.put("goodsItemEntity", goodsItemEntity);
		ClassesEntity classesEntity = new ClassesEntity();
		classesEntity.setType("goods");
		List<ClassesEntity> classesList = classesService
				.getClassesByType(classesEntity);
		modelMap.put("classesList", classesList);
		return "system/goods/update";
	}
	@AuthPassport
	@ResponseBody
	@RequestMapping(value = "/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(
			@ModelAttribute GoodsItemEntity goodsItemEntity,
			@RequestParam(value = "content", defaultValue = "") String content) throws Exception {
		JsonVo<String> json = new JsonVo<String>();
		if (goodsItemEntity.getTitle() == null
				|| "".equals(goodsItemEntity.getTitle())) {
			json.getErrors().put("title", "名称不能为空");
		}
		if (goodsItemEntity.getContent() == null
				|| "".equals(goodsItemEntity.getContent())) {
			json.getErrors().put("content", "内容不能为空");
		}
		if (goodsItemEntity.getClassId() == 0) {
			json.getErrors().put("classId", "所属栏目不能为空");
		}
		if (validateJsonIsError(json))
			return json;
		GoodsItemEntity oldGoodsItemEntity=goodsItemService.getById(goodsItemEntity.getGoodsId());
		if (null==oldGoodsItemEntity) {
			json.getErrors().put("title", "商品不存在");
			return json;
		}
		oldGoodsItemEntity.setProductPicUrl(goodsItemEntity.getProductPicUrl());
		oldGoodsItemEntity.setClassId(goodsItemEntity.getClassId());
		oldGoodsItemEntity.setProductPrice(goodsItemEntity.getProductPrice());
		oldGoodsItemEntity.setProductOriginalPrice(goodsItemEntity.getProductOriginalPrice());
		oldGoodsItemEntity.setContent(content);
		oldGoodsItemEntity.setUpdateTime(new Date());
		oldGoodsItemEntity.setTitle(goodsItemEntity.getTitle());
		oldGoodsItemEntity.setProductCommission(goodsItemEntity.getProductCommission());
		goodsItemService.update(oldGoodsItemEntity);
		json.setResult(true);
		return json;
	}

	@RequestMapping(value = "/preview.htm", method = RequestMethod.GET)
	public String preview(@RequestParam(value = "goodsId") long goodsId,
			@RequestParam(value = "p", defaultValue = "0") int p,
			ModelMap modelMap, HttpServletRequest request) throws Exception {
		return classesIndexAction.goodsItemDefault(goodsId, request,
				modelMap);
	}
	
	
}
